# Create your views here.
import logging
import random

from django import http
from django.views import View
from django_redis import get_redis_connection

# 图形验证码视图

# 创建日志输出器
from apps.verifications import constants
from apps.verifications.libs.captcha.captcha import captcha
from common.utils.response_code import RETCODE
from celery_tasks.sms.tasks import send_sms_code

# 创建日志输出器
logger = logging.getLogger('django')


class ImageCaptchaView(View):
    """图形验证码生成"""

    def get(self, request, uuid):
        """
        :param request: 请求对象
        :param uuid: 唯一标识图形验证码所属于的用户
        :return: image/jpg
        """

        # 生成图片验证码
        text, image = captcha.generate_captcha()

        # 保存图片验证码
        redis_conn = get_redis_connection('verify_captcha')
        redis_conn.setex('img_%s' % uuid, constants.IMAGE_CODE_REDIS_EXPIRES, text)

        return http.HttpResponse(image, content_type='image/jpg')


class ValidatorImageCodeView(View):
    """图形验证码比对"""

    def get(self, request, uuid, code):

        # # 校验参数
        # if not all([uuid, code]):
        #     return http.JsonResponse({'code': RETCODE.NECESSARYPARAMERR, 'errmsg': '缺少必传参数'})

        # 创建连接到redis的对象
        redis_conn = get_redis_connection('verify_captcha')
        # 提取图形验证码
        cache_captcha = redis_conn.get('img_%s' % uuid)

        if cache_captcha is None:
            # 图形验证码过期或者不存在
            return http.JsonResponse({'code': RETCODE.IMAGECODEERR, 'errmsg': '图形验证码失效'})

        # 删除图形验证码，避免恶意测试图形验证码
        # try:
        #     redis_conn.delete('img_%s' % uuid)
        # except Exception as e:
        #     logger.error(e)

        # 对比图形验证码
        cache_captcha = cache_captcha.decode()  # bytes转字符串

        if code.lower() != cache_captcha.lower():
            return http.JsonResponse({'code': RETCODE.IMAGECODEERR, 'errmsg': '输入图形验证码有误'})

        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': 'OK'})


class SMSCodeView(View):
    """短信验证码"""

    def get(self, request, mobile):
        """
        :param reqeust: 请求对象
        :param mobile: 手机号
        :return: JSON
        """

        # 创建连接到redis的对象
        redis_conn = get_redis_connection('verify_captcha')

        # 判断用户是否频繁发送短信验证码
        send_flag = redis_conn.get('send_flag_%s' % mobile)
        if send_flag:
            return http.HttpResponse({'code': RETCODE.THROTTLINGERR, 'errmsg': '发送短信过于频繁'})

        # 生成短信验证码：生成6位数验证码
        sms_code = '%06d' % random.randint(0, 999999)
        logger.info(sms_code)  # 手动的输出日志，记录短信验证码

        # # 保存短信验证码
        # redis_conn.setex('sms_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code)
        # # 重新写入send_flag
        # redis_conn.setex('send_flag_%s' % mobile, constants.SEND_SMS_CODE_INTERVAL, 1)

        # 创建redis管道
        pl = redis_conn.pipeline()
        # 将命令添加到队列中
        # 保存短信验证码
        pl.setex('sms_%s' % mobile, constants.SMS_CODE_REDIS_EXPIRES, sms_code)
        # 保存发送短信验证码的标记
        pl.setex('send_flag_%s' % mobile, constants.SEND_SMS_CODE_INTERVAL, 1)
        # 执行
        pl.execute()

        # # 发送短信验证码
        # CCP().send_template_sms(mobile, [sms_code, constants.SMS_CODE_REDIS_EXPIRES // 60],
        #                         constants.SEND_SMS_TEMPLATE_ID)
        # Celery异步发送短信验证码
        send_sms_code.delay(mobile, sms_code)

        # 响应结果
        return http.JsonResponse({'code': RETCODE.OK, 'errmsg': '发送短信成功'})
